import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class Map extends JPanel implements ActionListener, MouseListener, MouseMotionListener{

	double x = 0.0;
	double y = 0.0;
	Timer t;
	int food = 5;
	int metal = 25;
	int gold = 100;
	boolean bldg1 = false;
	boolean bldg2 = false;
	GroupOfBuildings buildings = new GroupOfBuildings();


	public Map(JFrame f){
		this.setBackground(new Color(245, 230, 222));
		this.addMouseMotionListener(this);
		this.addMouseListener(buildings);
		this.addMouseMotionListener(buildings);
		for(Building b: GroupOfBuildings.bldgs){
			this.addMouseListener(b);
			this.addMouseMotionListener(b);
		}
		t = new Timer(10, this);
		t.start();
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Conquest");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1280, 950);
		frame.setResizable(false);
		Map e = new Map(frame);
		frame.add(e);
		frame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(t)){
			this.repaint();
		}
	}

	@Override
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;

		super.paint(g2d);

		g2d.setColor(new Color(0, 50, 100));
		g2d.fillRect(0, 800, 1280, 950);
		g2d.setColor(new Color(0, 200, 50));
		g2d.fillOval(0, 0, 300, 300);
		g2d.fillOval(980, 500, 300, 300);
		Font f = new Font(Font.SANS_SERIF, 3, 36);
		g2d.setFont(f);
		g2d.setColor(new Color(255, 215, 0));
		g2d.drawString("GOLD: "+gold, 0, 875);
		g2d.setColor(new Color(200, 0, 50));
		g2d.drawString("FOOD: "+food, 250, 875);
		g2d.setColor(new Color(192, 192, 192));
		g2d.drawString("METAL: "+metal, 450, 875);
		g2d.setColor(new Color(0, 50, 255));
		g2d.fillRect(100, 100, 100, 100);
		g2d.setColor(new Color(255, 50, 0));
		g2d.fillRect(1080, 600, 100, 100);
		
		if(!bldg1){
			try {
				BufferedImage bi = ImageIO.read(new File("src/res/hammer.gif"));
				g2d.setColor(new Color(0, 0, 0));
				g2d.fillRoundRect(675, 815, 100, 100, 25, 25);
				g2d.setColor(new Color(255, 0, 0));
				g2d.fillRect(675 + 50/3, 815 + 50/3, 100/3, 100/3);
				g2d.fillRect(675 + 150/3, 815 + 150/3, 100/3, 100/3);
				g2d.drawImage(bi, 675 + 150/3, 815 + 50/3, 100/3, 100/3, null);
				g2d.drawImage(bi, 675 + 50/3, 815 + 150/3, 100/3, 100/3, null);
			} catch (IOException e) {
				System.out.println("ERROR 404: File not found");
			}
		}
		else if(bldg1){
			g2d.setColor(new Color(0, 0, 0));
			g2d.fillRoundRect(675, 815, 100, 100, 25, 25);
			g2d.setColor(new Color(125, 0, 0));
			Font fo = new Font(Font.SANS_SERIF, 3, 16);
			g2d.setFont(fo);
			g2d.drawString("BARRACKS", 680, 875);
		}
		if(!bldg2){
			try {
				BufferedImage bi = ImageIO.read(new File("src/res/hammer.gif"));
				g2d.setColor(new Color(0, 125, 25));
				g2d.fillRoundRect(800, 815, 100, 100, 25, 25);
				g2d.setColor(new Color(255, 0, 0));
				g2d.fillRect(800 + 50/3, 815 + 50/3, 100/3, 100/3);
				g2d.fillRect(800 + 150/3, 815 + 150/3, 100/3, 100/3);
				g2d.drawImage(bi, 800 + 150/3, 815 + 50/3, 100/3, 100/3, null);
				g2d.drawImage(bi, 800 + 50/3, 815 + 150/3, 100/3, 100/3, null);
			} catch (IOException e) {
				System.out.println("ERROR 404: File not found");
			}
		}
		else if(bldg2){
			g2d.setColor(new Color(0, 125, 5));
			g2d.fillRoundRect(800, 815, 100, 100, 25, 25);
			g2d.setColor(new Color(255, 0, 0));
			Font fo = new Font(Font.SANS_SERIF, 3, 32);
			g2d.setFont(fo);
			g2d.drawString("FARM", 803, 875);
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent arg0) {

		if(arg0.getX() >= 675 && arg0.getY() >=  815 && arg0.getX() <= 775 && arg0.getY() <= 915){
			bldg1 = true;
		}
		else{
			bldg1 = false;
		}
		if(arg0.getX() >= 800 && arg0.getY() >=  815 && arg0.getX() <= 900 && arg0.getY() <= 915){
			bldg2 = true;
		}
		else{
			bldg2 = false;
		}
	}
}
